package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.common.utils.VerifyCodeUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.mapper.UserInfoMapper;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.minio.credentials.Jwt;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SmsService smsService;
    @Autowired
    private  StringRedisTemplate redisTemplate;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void getCode(String phone) {

        String verifyCode = VerifyCodeUtil.generateVerifyCode(6);
        smsService.sndSms(phone,verifyCode);
        String key = RedisConstant.APP_LOGIN_PREFIX + phone;
        Boolean result = redisTemplate.hasKey(key);
        if (result){
            Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
            Integer totalTime = RedisConstant.APP_LOGIN_CODE_TTL_SEC;
            if ((totalTime - expire)<RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC){
                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
            }
        }
        redisTemplate.opsForValue().set(key,verifyCode,RedisConstant.APP_LOGIN_CODE_TTL_SEC,TimeUnit.SECONDS);//,RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS
    }

    @Override
    public String login(LoginVo loginVo) {
        String phone = loginVo.getPhone();
        if (phone==null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }
        String code = loginVo.getCode();
        if (!StringUtils.hasText(code)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        String key = RedisConstant.APP_LOGIN_PREFIX + phone;
        String redisCode = redisTemplate.opsForValue().get(key);
        if (redisCode==null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaUpdateWrapper<UserInfo>().eq(UserInfo::getPhone, phone));
        if (userInfo == null){
            userInfo = new UserInfo();
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfo.setPhone(phone);
            userInfo.setNickname("用户-"+phone.substring(6));
            userInfoMapper.insert(userInfo);
        }
        if (userInfo.getStatus()==BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        String token = JwtUtil.creatToken(phone, userInfo.getId());
        return token;
    }
}
